package mysql

import (
	"video-hub/videorpc/videorpc"

	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	VideoId    int64  `gorm:"type:int(11);not null;comment:视频ID"`
	UserId     int64  `gorm:"type:int(11);not null;comment:用户ID"`
	Content    string `gorm:"type:varchar(255);not null;comment:评论内容"`
	IsNonymous string `gorm:"type:tinyint(1);default:0;not null;comment:是否匿名,0-非匿名，1-匿名"`
	Status     string `gorm:"type:varchar(255);default:0;not null;comment: 评论状态,0-有效，1-违规（含敏感词）"`
	ReplyId    int64  `gorm:"type:int(11);not null;comment:回复ID"`
}

func (c *Comment) AddMen(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) DelMen(db *gorm.DB, id int64) error {
	return db.Debug().Where("comments.id = ?", id).Delete(c).Error
}

func (c *Comment) FindMenId(db *gorm.DB, id int64) error {
	return db.Debug().Where("comments.id = ?", id).First(c).Error
}

func MenList(db *gorm.DB, in *videorpc.MenListRequest) []*videorpc.MenList {
	var menList []*videorpc.MenList
	db.Debug().Model(&Comment{}).Select("users.id as user_id,users.nick_name,comments.content,comments.created_at").
		Joins("LEFT JOIN users ON comments.user_id = users.id").Order("comments.created_at desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&menList)
	return menList
}
